<?php

require_once('configuration/pdo/AbstractPdoConfig.class.php');

class RemoteMysqlPdoConfig extends AbstractPdoConfig {
	
	private $createTableStatements;
	
	private $host;
	private $dbname;
	private $user;
	private $pass;
	
	public function RemoteMysqlPdoConfig($host, $dbname, $user, $pass) {
		$this->host = $host;
		$this->dbname = $dbname;
		$this->user = $user;
		$this->pass  = $pass;
		
		$this->createTableStatements = array(
			TBL_USER => 'CREATE TABLE IF NOT EXISTS ' . TBL_USER . ' ( ' .
				'user_id INTEGER PRIMARY KEY AUTO_INCREMENT, ' .
				'username VARCHAR(80) NOT NULL, ' .
				'password VARCHAR(80) NOT NULL, ' .
				'email VARCHAR(80) NOT NULL ' .
			')',
			TBL_VOTE => 'CREATE TABLE IF NOT EXISTS ' . TBL_VOTE . ' ( ' .
				'user_id INTEGER NOT NULL, ' .
				'topic_id INTEGER NOT NULL, ' .
				'created TIMESTAMP NOT NULL, ' .
				'PRIMARY KEY (user_id, topic_id),' . 
				'FOREIGN KEY (user_id) REFERENCES ' . TBL_USER . ' (user_id), ' .
				'FOREIGN KEY (topic_id) REFERENCES ' . TBL_TOPIC . ' (topic_id) ' .
			')',
			
			TBL_TOPIC => 'CREATE TABLE IF NOT EXISTS ' . TBL_TOPIC . ' ( ' .
				'topic_id INTEGER PRIMARY KEY AUTO_INCREMENT, ' .
				'user_id INTEGER NOT NULL, ' .
				'title VARCHAR(255) NOT NULL, ' .
				'created TIMESTAMP NOT NULL, ' .
			
				'FOREIGN KEY (user_id) REFERENCES ' . TBL_USER . ' (user_id) ' .
			')',
			TBL_OFFERED_TOPIC => 'CREATE TABLE IF NOT EXISTS ' . TBL_OFFERED_TOPIC . ' ( ' .
				'offered_topic_id INTEGER PRIMARY KEY AUTO_INCREMENT, ' .
				'topic_id INTEGER NOT NULL, ' .
				'offered_category_id INTEGER NOT NULL, ' .
				'FOREIGN KEY (topic_id) REFERENCES ' . TBL_TOPIC . ' (topic_id), ' .
				'FOREIGN KEY (offered_category_id) REFERENCES ' . TBL_OFFERED_CATEGORY . ' (offered_category_id) ' .
			')',
			TBL_NEEDED_TOPIC => 'CREATE TABLE IF NOT EXISTS ' . TBL_NEEDED_TOPIC . ' ( ' .
				'needed_topic_id INTEGER PRIMARY KEY AUTO_INCREMENT, ' .
				'topic_id INTEGER NOT NULL, ' .
				'needed_category_id INTEGER NOT NULL, ' .
				'FOREIGN KEY (topic_id) REFERENCES ' . TBL_TOPIC . ' (topic_id), ' .
				'FOREIGN KEY (needed_category_id) REFERENCES ' . TBL_CATEGORY . ' (needed_category_id) ' .
			')',
			
			TBL_CATEGORY => 'CREATE TABLE IF NOT EXISTS ' . TBL_CATEGORY . ' ( ' .
				'category_id INTEGER PRIMARY KEY AUTO_INCREMENT, ' .
				'title VARCHAR(255) NOT NULL' .
			')',
			TBL_OFFERED_CATEGORY => 'CREATE TABLE IF NOT EXISTS ' . TBL_OFFERED_CATEGORY . ' ( ' .
				'offered_category_id INTEGER PRIMARY KEY AUTO_INCREMENT, ' .
				'category_id INTEGER NOT NULL, ' .
				'FOREIGN KEY (category_id) REFERENCES ' . TBL_CATEGORY . ' (category_id) ' .
			')',
			TBL_NEEDED_CATEGORY => 'CREATE TABLE IF NOT EXISTS ' . TBL_NEEDED_CATEGORY . ' ( ' .
				'needed_category_id INTEGER PRIMARY KEY AUTO_INCREMENT, ' .
				'category_id INTEGER NOT NULL, ' .
				'FOREIGN KEY (category_id) REFERENCES ' . TBL_CATEGORY . ' (category_id) ' .
			')'
		);	
	}
	
	protected function _newPDO() {
		$dns = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
		Log::instance()->debug("Connecting as [" . $this->user . "] with dns [$dns].");
		
		$pdo = new PDO($dns, $this->user, $this->pass);
		if($pdo === null) {
			throw new Exception('Could not connect with dns [' . $dns . ']!');
		}
		return $pdo;
	}
	
	public function getCreateStatement($tableName) {
		$res = $this->createTableStatements[$tableName];
		if($res == null) {
			throw new Exception('Invalid table name [' . $tableName . ']!');
		}
		return $res;
	}
	
}

?>